package org.sixtynine.stock.controller.front;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.sixtynine.stock.Constants.ChartType;
import org.sixtynine.stock.Constants.CommonData;
import org.sixtynine.stock.criteria.StockSearchCriteria;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.model.ColData;
import org.sixtynine.stock.model.ColModel;
import org.sixtynine.stock.model.GridData;
import org.sixtynine.stock.service.StockManagerService;
import org.sixtynine.stock.service.content.DataManagerService;
import org.sixtynine.stock.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilanka
 * 
 */
@Controller
public class PricePerformenceController extends AbstractGridController {

	@Autowired
	private StockManagerService service;

	@Autowired
	private DataManagerService dataManagerService;

	@RequestMapping(value = "/priceperformence/main")
	public ModelAndView showResults() {
		ModelAndView modelAndView = new ModelAndView("/priceperformence/main");
		modelAndView.addObject("criteria", new StockSearchCriteria());

		List<BaseEntity> compaies = dataManagerService
				.getData(CommonData.COMPANY.toString());
		modelAndView.addObject("companyList", compaies);
		showBannar(modelAndView);
		return modelAndView;
	}

	@RequestMapping(value = "/priceperformence/list", method = RequestMethod.POST)
	@ResponseBody
	public GridData getData(
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "rows", required = false, defaultValue = "20") int rows,
			@RequestParam(value = "sidx", required = false) String sidx,
			@RequestParam(value = "sord", required = false) String sord,
			@RequestParam(value = "search", required = false) String search,
			@RequestParam(value = "lastDate", required = false) String lastDate,
			@ModelAttribute StockSearchCriteria criteria,
			HttpServletRequest request) {

		if (!StringUtils.isEmpty(lastDate)) {
			try {
				Date lastRequestedDate = DateUtil.getDate(
						DateUtil.DB_DATE_FORMAT, lastDate);
				criteria._setLastDate(lastRequestedDate);
			} catch (ParseException e) {
			}
		}
		criteria._setAddAsiDetails(true);
		criteria.setChartType(ChartType.GENERAL);
		return getGridData(criteria, page, rows, sidx, sord, search, request);

	}

	@RequestMapping(value = "/priceperformence/colData", method = RequestMethod.GET)
	@ResponseBody
	public ColData getColumnData() {
		return getColData();
	}

	@Override
	public ColData getColData() {
		ColData colData = new ColData();

		List<ColModel> colModels = new ArrayList<ColModel>();

		colModels.add(new ColModel("ticker"));
		colModels.add(new ColModel("currentSharePrice", "number", "right"));
		colModels.add(new ColModel("sharePrice", "number", "right"));
		colModels.add(new ColModel("change", "number", "right"));
		colModels.add(new ColModel("oneWeekPricePer", "number", "right"));
		colModels.add(new ColModel("oneMonthPricePer", "number", "right"));
		colModels.add(new ColModel("threeMonthPricePer", "number", "right"));
		colModels.add(new ColModel("sixMonthPricePer", "number", "right"));
		colModels.add(new ColModel("twelMonthPricePer", "number", "right"));
		colModels.add(new ColModel("ytdPricePer", "number", "right"));
		colModels.add(new ColModel("twelMonthHigh", "number", "right"));
		colModels.add(new ColModel("twelMonthLow", "number", "right"));
		colModels.add(new ColModel("per", "number", "right"));
		colModels.add(new ColModel("pbv", "number", "right"));
		colModels.add(new ColModel("dy", "number", "right"));
		colModels.add(new ColModel("volume", "integer", "right"));
		colModels.add(new ColModel("avarageVolume", "number", "right"));
		colModels.add(new ColModel("rsi", "number", "right"));
		colModels.add(new ColModel("thertyMonthBeta", "number", "right"));
		colData.setColModel(colModels);

		List<String> colNames = new ArrayList<String>();

		colNames.add("Ticker / Index");
		colNames.add("Current price");
		colNames.add("LastDay price");
		colNames.add("Change %");
		colNames.add("1w");
		colNames.add("1M");
		colNames.add("3M");
		colNames.add("6M");
		colNames.add("12M");
		colNames.add("Ytd");
		colNames.add("12M High");
		colNames.add("12M Low");
		colNames.add("PER");
		colNames.add("PBV");
		colNames.add("DY %");
		colNames.add("Current Volume");
		colNames.add("3M Ava Volume");
		colNames.add("Rsi");
		colNames.add("36M B");
		colData.setColNames(colNames);
		return colData;
	}

}
